package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Sprots;
import com.itheima.service.SportService;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RequestMapping("/sport")
@RestController
public class SportController {
    @Reference
    private SportService sportService;

    //添加运动库
    @RequestMapping("/addSport")
    public Result addSport(@RequestBody Sprots sport) {
        try {
            sportService.addSport(sport);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_SPORT_FALL);
        }
        return new Result(true, MessageConstant.ADD_SPORT_SUCCESS);
    }

    //分页查询方法
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return sportService.findPage(queryPageBean);
    }
    //按id删除运动库
    @RequestMapping("/deleteById")
    public Result deleteById(Integer id){
        try {
            sportService.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_SPORT_FALL);
        }
        return new Result(true,MessageConstant.DELETE_SPORT_SUCCESS);
    }
    //编辑
    @RequestMapping("/edit")
    public Result edit(@RequestBody Sprots sport){
        try {
            sportService.edit(sport);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_SPORT_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_SPORT_SUCCESS);
    }

    //按照id查询
    @RequestMapping("/findSportById")
    public Result findSportById(Integer id){
        try {
            Sprots sport = sportService.findSportById(id);
            return new Result(true,MessageConstant.QUERY_SPORT_SUCCESS,sport);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_SPORT_FAIL);
        }
    }
}
